<?php
    include_once './config.php';
    $user = checkLogin(3);
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $error = false;
        $password = $_POST['password'];
        $old_password = $_POST['old_password'];
        if(md5($old_password) != $user['password']){
            $error = true;
            $msg = "密码不正确";
        } else {
            $query = "update whw_user set password = ".quote(md5($password))." where id=".getInt($user['id']);
            $objDBCD14->execute($query);
            $msg = "设置成功";
        }
        echo json_encode(['code'=>$error ? 0 : 1,'msg'=>$msg]);
        exit;
    }
    $faceList = [];
    $user_id = $_GET['user_id'];
    $query = "select * from whw_face where user_id=".getInt($user_id);
    $rows = $objDBCD14->query($query);
    if ($rows->num_rows) {
        foreach ($rows as $key => $face) {
            $faceList[] =  $face; 
        }  
    }
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>设置密码</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        .container{
            margin: 0;
            padding: 10px 10px 20px;
            box-sizing: border-box;
            width: 100%;
            background: #f8f8f8; 
            height:100%;
        }
        header{
            display: flex;
            align-items: center;
            background: #e64340;
            padding: 12px;
            color: #fff;
            justify-content: space-between;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 44px;
            box-sizing: border-box;
        }
        header .title{
            text-align: center;
        }
        header .layui-icon{
            font-size: 18px;
        }
        main{
            padding: 44px 0 0;
        }
        .form{
            background: #fff;
            padding: 30px 10px;
            margin: 10px;
            border-radius: 5px;
        }
        .layui-form-label{
            width: 70px;
            padding: 10px 0;
        }
        .layui-input-block{
            margin-left: 90px;
        }
    </style>
</head>
<body ontouchstart>
    <div class="container" id="container">
        <header>
            <div>
                <i class="layui-icon layui-icon-left" onclick="history.back()"></i>
            </div>
            <div class="title">设置密码</div>
            <div class="add-btn"></div>
        </header>
        <main>
           <div class="form">
                <form class="password-form" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">原密码</label>
                        <div class="layui-input-block">
                          <input type="password" name="old_password" required  lay-verify="required" placeholder="请输入原密码" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">新密码</label>
                        <div class="layui-input-block">
                          <input type="password" name="password" required  lay-verify="required" placeholder="请输入新密码" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">确认密码</label>
                        <div class="layui-input-block">
                          <input type="password" name="password_again" required  lay-verify="required" placeholder="请输入确认密码" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                     <button class="layui-btn layui-btn-fluid  layui-btn-lg"
                     style="margin-top: 20px"> 确认</button>
                </form>
           </div>
        </main>
    </div>
    <!-- 引入 layui.js -->
    <script src="./layui/layui.js"></script>
    <script>
        layui.use(['jquery','layer', 'form','code'], function(){
            var $ = layui.$,layer = layui.layer,form = layui.form,code = layui.code;
             $(".password-form").submit(function(){
                var formValues = $(this).serializeArray();
                var param = {}
                for(var i = 0;i < formValues.length;i++){
                    param[formValues[i].name] = formValues[i].value
                }
                if(param.password != param.password_again){
                    layer.msg("两次密码输入不一致");
                    return false
                }
                $.post("password.php",param,function(res){
                    res = JSON.parse(res)
                    layer.msg(res.msg);
                    if(res.code == 1){
                        setTimeout(function(){
                            location.reload()
                        },1000)
                    }
                })
                return false
             })
        });
    </script>  
</body>
</html>